import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class SemaphoreDemo2 {
    private static Semaphore semaphore = new Semaphore(0);

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 2; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread()+ "开始执行");
                    semaphore.release();
                }
            });
        }
        // 等待子线程执行完毕，返回
        semaphore.acquire(2);
        System.out.println("所有线程执行完毕");

        // 关闭线程池
        executorService.shutdown();
    }
}
